import numpy as np
from sklearn.datasets import load_boston,load_diabetes
import matplotlib.pyplot as plt

def get_Loss(dataset):
    targets = dataset[:,-1]
    return np.var(targets)*len(targets)

def chooseBestFeature(dataset,features):
    """
        :param dataset: 数据集
        :param features: 数据的属性
        :return: 返回最优的属性
        """
    toCart = True

    min_var = get_Loss(dataset)

    # 最优属性
    best_feature = 0
    # 所有属性中的最大信息增益
    min_gain = 9999999
    # 每个属性的最佳划分点
    best_huafen = []
    for feature in features:
        # 当前属性中的最大信息增益
        min_gain_part = 9999999
        featureList = np.sort(dataset[:, feature])
        # 取平均
        T_midu = set([(featureList[i] + featureList[i + 1]) / 2 for i in range(len(featureList) - 1) if
                      featureList[i] != featureList[i + 1]])
        best_huafen.append(0)
        for feat in T_midu:
            p = min_var
            # 比划分点小的数据
            dataset_smaller = dataset[dataset[:, feature] < feat]
            ent_s = get_Loss(dataset_smaller)
            # 比划分点大的数据
            dataset_bigger = dataset[dataset[:, feature] > feat]
            ent_b = get_Loss(dataset_bigger)
            # 计算信息增益
            gain = ent_s + ent_b
            if (min_gain_part > gain):
                min_gain_part = gain
                best_huafen[feature] = feat
        if min_gain > min_gain_part:
            best_feature = feature
            min_gain = min_gain_part
    # 如果不划分的效果更好，则不划分
    if min_var<min_gain:
        toCart = False

    return best_feature, best_huafen[best_feature], toCart

def createRegresionTree(dataset,features):
    targets = dataset[:, -1]
    # 如果所有标签都一样就不需要继续分叉

    # 获得最优属性
    best_feature, best_huafen, toCart = chooseBestFeature(dataset, range(len(dataset[0])-1))

    if not toCart or len(dataset)<5:
        return np.mean(targets)

    regresionTree = {best_feature: {}}

    regresionTree[best_feature]["<{}".format(best_huafen)] = createRegresionTree(
        dataset[dataset[:, best_feature] < best_huafen], features)
    regresionTree[best_feature][">{}".format(best_huafen)] = createRegresionTree(
        dataset[dataset[:, best_feature] > best_huafen], features)

    return regresionTree

def test(data,tree,features):
    while True:
        for feat in features:
            if feat not in tree:
                continue
            tree = tree[feat]
            f = data[feat]
            keys = list(tree.keys())
            num = float(keys[0][1:])
            tree = tree[keys[0]] if f<num else tree[keys[1]]
            if type(tree) == np.float64:
                return tree

def predict(dataset,tree,features):
    targets = dataset[:,-1]
    ans = [test(data,tree,features) for data in dataset]
    ans = np.array(ans)
    x = dataset[:,1]
    # 横坐标排序的索引
    x_sort = np.argsort(x)
    plt.plot(x,targets,'r.')
    plt.plot(x[x_sort],ans[x_sort],'b')
    plt.show()

def split_dataset(dataset,rate):
    """
    :param dataset: 数据集
    :param rate: 训练集的比率
    :return: 返回分割好的训练集和测试集
    """
    np.random.shuffle(dataset)
    return dataset[:int(len(dataset)*rate)],dataset[int(len(dataset)*rate):]


def getNumLeafs(myTree):
    numLeafs = 0
    firstStr = list(myTree.keys())[0]
    secondDict = myTree[firstStr]
    for key in secondDict.keys():
        if type(secondDict[
                    key]).__name__ == 'dict':  # test to see if the nodes are dictonaires, if not they are leaf nodes
            numLeafs += getNumLeafs(secondDict[key])
        else:
            numLeafs += 1
    return numLeafs


def getTreeDepth(myTree):
    maxDepth = 0
    firstStr = list(myTree.keys())[0]
    secondDict = myTree[firstStr]
    for key in secondDict.keys():
        if type(secondDict[
                    key]).__name__ == 'dict':  # test to see if the nodes are dictonaires, if not they are leaf nodes
            thisDepth = 1 + getTreeDepth(secondDict[key])
        else:
            thisDepth = 1
        if thisDepth > maxDepth: maxDepth = thisDepth
    return maxDepth


def plotNode(nodeTxt, centerPt, parentPt, nodeType):
    createPlot.ax1.annotate(nodeTxt, xy=parentPt, xycoords='axes fraction',
                            xytext=centerPt, textcoords='axes fraction',
                            va="center", ha="center", bbox=nodeType, arrowprops=arrow_args)


def plotMidText(cntrPt, parentPt, txtString):
    xMid = (parentPt[0] - cntrPt[0]) / 2.0 + cntrPt[0]
    yMid = (parentPt[1] - cntrPt[1]) / 2.0 + cntrPt[1]
    createPlot.ax1.text(xMid, yMid, txtString, va="center", ha="center", rotation=30)


def plotTree(myTree, parentPt, nodeTxt):  # if the first key tells you what feat was split on
    numLeafs = getNumLeafs(myTree)  # this determines the x width of this tree
    depth = getTreeDepth(myTree)
    firstStr = list(myTree.keys())[0]  # the text label for this node should be this
    cntrPt = (plotTree.xOff + (1.0 + float(numLeafs)) / 2.0 / plotTree.totalW, plotTree.yOff)
    plotMidText(cntrPt, parentPt, nodeTxt)
    plotNode(firstStr, cntrPt, parentPt, decisionNode)
    secondDict = myTree[firstStr]
    plotTree.yOff = plotTree.yOff - 1.0 / plotTree.totalD
    for key in secondDict.keys():
        if type(secondDict[
                    key]).__name__ == 'dict':  # test to see if the nodes are dictonaires, if not they are leaf nodes
            plotTree(secondDict[key], cntrPt, str(key))  # recursion
        else:  # it's a leaf node print the leaf node
            plotTree.xOff = plotTree.xOff + 1.0 / plotTree.totalW
            plotNode(secondDict[key], (plotTree.xOff, plotTree.yOff), cntrPt, leafNode)
            plotMidText((plotTree.xOff, plotTree.yOff), cntrPt, str(key))
    plotTree.yOff = plotTree.yOff + 1.0 / plotTree.totalD


# if you do get a dictonary you know it's a tree, and the first element will be another dict

def createPlot(inTree, name):
    fig = plt.figure(1, facecolor='white')
    fig.clf()
    axprops = dict(xticks=[], yticks=[])
    createPlot.ax1 = plt.subplot(111, frameon=False, **axprops)  # no ticks
    # createPlot.ax1 = plt.subplot(111, frameon=False) #ticks for demo puropses
    plotTree.totalW = float(getNumLeafs(inTree))
    plotTree.totalD = float(getTreeDepth(inTree))
    plotTree.xOff = -0.5 / plotTree.totalW;
    plotTree.yOff = 1.0;
    plotTree(inTree, (0.5, 1.0), '')
    # plt.savefig('13数据分布情况')
    plt.savefig(str(name))
    plt.show()

if __name__ == '__main__':
    decisionNode = dict(boxstyle="sawtooth", fc="0.8")
    leafNode = dict(boxstyle="round4", fc="0.8")
    arrow_args = dict(arrowstyle="<-")
    # 加载糖尿病数据集
    diabetes = load_diabetes()
    data, targets = diabetes.data, diabetes.target
    # 将标签和数据拼接，方便处理数据
    dataset = np.hstack((data, targets.reshape((targets.shape[0], 1))))

    # 对数据集进行分割
    train_dataset, test_dataset = split_dataset(dataset, 0.7)

    # 建立回归树
    RegresionTree = createRegresionTree(train_dataset, range(len(data[0])))
    print(RegresionTree)
    createPlot(RegresionTree,'糖尿病数据集回归树')
    # 测试
    predict(test_dataset, RegresionTree, range(len(data[0])))

    # 加载波士顿数据集
    boston = load_boston()
    data, targets = boston.data, boston.target
    # 将标签和数据拼接，方便处理数据
    dataset = np.hstack((data, targets.reshape((targets.shape[0], 1))))

    # 对数据集进行分割
    train_dataset, test_dataset = split_dataset(dataset, 0.7)

    # 建立回归树
    RegresionTree = createRegresionTree(train_dataset, range(len(data[0])))
    print(RegresionTree)
    # 测试
    predict(test_dataset, RegresionTree, range(len(data[0])))
    createPlot(RegresionTree, '波士顿房价数据集回归树')